﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using System.Collections.Generic;
using Cn.Cellula.Client.ControlHelper;
using Cn.Edwon.Web;
using Cn.Cellula.Model;
using System.Text;
using Cn.Cellula.Biz;
using Cn.Cellula.Model.Security;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Permission.Role {
    public partial class List : PageSupport {
        IUserBiz biz = BizFactory.Get<IUserBiz>();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                LoadFuncsData();
                LoadRoleList();
            }
        }

        private void LoadRoleList() {
            IList<TSysRoleEO> list = biz.LoadAllRoles(this.Power.RoleLevel);
            this.gvRoleList.DataSource = list;
            this.gvRoleList.DataBind();
            if (list != null && list.Count > 0) {
                GvHelper.GvSelected(ref this.gvRoleList, list[0].RoleKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
                this.hid_SelectedRoleKey.Value = list[0].RoleKey;
                LoadFuncs(list[0].RoleKey);
            }
        }

        private void LoadFuncsData() {
            this.lab_FuncData.Text = "<script type=\"text/javascript\">var FuncData = " + biz.LoadFunctions2Json(this.Actor,"") + ";</script>";
        }

        private void LoadFuncs(String rRoleKey) {
            StringBuilder sb = new StringBuilder("var RoleFunc = [");
            IList<TSysRoleFunctionEO> list = biz.LoadRfByRole(rRoleKey);
            foreach (TSysRoleFunctionEO eo in list) {
                sb.Append("\""+eo.FunctionKey+"\"");
                sb.Append(",");
            }
            sb.Append("\"0\"];");
            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "", sb.ToString(), true);
        }


        protected void btn_RoleAdd_Click(object sender, EventArgs e) {
            Response.Redirect("Edit.aspx");
        }

        protected void btn_RoleUpdate_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvRoleList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys.Count > 0) {
                Response.Redirect("Edit.aspx?ID=" + keys[0] + "");
            } else {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoEditSelect);
                return;
            }
        }

        protected void btn_RoleDel_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvRoleList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys.Count < 1) {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.NoDelSelect);
                return;
            }
            //如果有用户绑定了该角色，则不可删除
            foreach (String k in keys) {
                TSysRoleEO eo = biz.Load<TSysRoleEO>(k);
                if (biz.IfRoleInUse(k)) {
                    SiteInfo.Send(Page, TxtDef.ERROR, "角色("+eo.RoleName+")已被使用，无法删除");
                    break;
                }
                if (eo.RoleKey != BizConst.Admin.ROLE_KEY) {
                    biz.Delete<TSysRoleEO>(k);
                    BizFactory.Get<ILogBiz>().LogDelete("角色:"+eo.RoleName, 1, this.Actor, Request);
                }
            }
            LoadRoleList();
        }

        protected void gvRoleList_RowCommand(object sender, GridViewCommandEventArgs e) {
            if (e.CommandName == "CheckFunc") {
                String roleKey = e.CommandArgument.ToString();
                this.hid_SelectedRoleKey.Value = roleKey;
                this.LoadFuncs(roleKey);
                GvHelper.GvSelected(ref this.gvRoleList, roleKey,ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            }
        }

        protected void btn_Bind_Click(object sender, EventArgs e) {
            if (String.IsNullOrEmpty(this.hid_SelectedRoleKey.Value)) {
                SiteInfo.Send(Page, TxtDef.ERROR, "请先选择一个角色");
                return;
            }
            try {
                String funcs = Request.Form["cbx_function"];
                if (!String.IsNullOrEmpty(funcs)) {
                    biz.BindFunc2Role(funcs.Split(','), this.hid_SelectedRoleKey.Value);
                    BizFactory.Get<ILogBiz>().Info("设定了角色(" + this.hid_SelectedRoleKey.Value + ")与功能的绑定", this.Actor, Request);
                }
            } catch {
                SiteInfo.Send(Page, TxtDef.ERROR, TxtDef.FAILED);
                return;
            }

            LoadFuncs(this.hid_SelectedRoleKey.Value);
            SiteInfo.Send(Page, TxtDef.INFO,TxtDef.SUCCEED);
        }
    }
}
